# encoding: utf-8
"""
酷安APP请求参数token破解
@Author:      "June"
@CreateTime:  2020/5/27 1:38 
@File:        coolapk_token
@Project:     PyCharm
"""
import os

import frida


class CoolapkGetToken:
    def __init__(self):
        super(CoolapkGetToken, self).__init__()
        self.token = None

    def tcp_mapping(self):
        os.system('adb forward tcp:27043 tcp:27043')
        os.system('adb forward tct:27042 tcp:27042')

    def on_message(self, message, data):
        if message['type'] == 'send':
            print(message['payload'])
            if len(message['payload']) > 40:
                self.token = message['payload']
        elif message['type'] == 'error':
            print(message['stack'])

    def get_token(self):
        script_str = """
            rpc.exports = {
                gettoken: function() {
                    send("Start inject hook code.");
                    Java.perform(function(){
                        //拿到context上下文
                        var currentApplication = Java.use("android.app.ActivityThread").currentApplication();
                        var context = currentApplication.getApplicationContext();
                        //send(context);
                        var authUtils = Java.use("com.coolapk.market.util.AuthUtils");
                        var token = authUtils.getAS(context, "sdfsgdhwer36475sf2354656");
                        //return token;
                        send(token);
                    })
                }
            }
        """
        # tcp_mapping()

        process = frida.get_usb_device().attach('com.coolapk.market')     # apk包名
        script = process.create_script(script_str)
        script.on('message', self.on_message)
        script.load()

        return script.exports.gettoken()

if __name__ == '__main__':
    cool = CoolapkGetToken()
    cool.get_token()
    print(cool.token)
